//
//  DetailDealTool.m
//  Daybook
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "DetailDealTool.h"
#import "CalendarTool.h"
#import "Common.h"


@interface DetailDealTool ()


@end
@implementation DetailDealTool

+ (DetailDealTool *)shareDetailTool {
    static DetailDealTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[DetailDealTool alloc] init];
    });
    return tool;
}

- (NSMutableArray *)arrayFromDataBaseJudgeByDate:(NSDate *)date {
    NSString *str = @"2016-05-23";
    
    
//    NSLog(@"此时的Date: %@", [[CalendarTool shareCalendarTool] stringFromDate:date]);
    
    NSMutableArray *newWeekArr = [NSMutableArray array];

    
    NSDate *todayDate = [[CalendarTool shareCalendarTool] dateFromString:str];
    
    NSInteger year = [[CalendarTool shareCalendarTool] year:date];
    NSInteger month = [[CalendarTool shareCalendarTool] month:date];
    NSInteger day = [[CalendarTool shareCalendarTool] day:date];
    
    NSArray *weekArr = [[CalendarTool shareCalendarTool] arrayMonthWeekendFromDate:date];
    
    
    for (NSInteger i = 0; i < weekArr.count; i++) {
        // detailArr开始
        
        NSMutableArray *detailArr = [NSMutableArray array];
        NSMutableDictionary *detailDic = [NSMutableDictionary dictionary];
        NSMutableDictionary *sectionDic = [NSMutableDictionary dictionary];
        NSMutableArray *cellArr = [NSMutableArray array];
        
        [detailArr addObject:detailDic];
        [detailDic setValue:sectionDic forKey:@"section"];
        [detailDic setValue:cellArr forKey:@"cell"];
        
        double allIncome = 0;
        double allPay = 0;
        
        NSString *weekStr = weekArr[i];
        NSArray *tempArr = [weekStr componentsSeparatedByString:@"-"];
    
        NSString *beginStr = [(NSString *)(tempArr[0]) stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%02ld.", month] withString:@""];
        NSString *endStr = [(NSString *)(tempArr[1]) stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%02ld.", month] withString:@""];
        
        for (NSInteger i = endStr.integerValue; i >= beginStr.integerValue; i--) {
            NSMutableArray *dayArr = [NSMutableArray array];
            // 取出每一天的数据
            NSString *tempStr = [NSString stringWithFormat:@"%04ld-%02ld-%02ld", year, month, i];
            NSDate *tempDate = [[CalendarTool shareCalendarTool] dateFromString:tempStr];
            NSMutableArray *dataBaseArr = [[CalendarTool shareCalendarTool] AccountBookModelArrayFromSmallDate:tempDate];

            for (NSInteger j = 0; j < dataBaseArr.count; j++) {
                Account *model = dataBaseArr[j];
                NSMutableDictionary *dayDic = [NSMutableDictionary dictionary];

                [dayDic setValue:model.picName forKey:@"image"];
                
                NSMutableDictionary *countDic = [NSMutableDictionary dictionary];
                [dayDic setValue:model.picName forKey:@"image"];
                [dayDic setValue:countDic forKey:@"count"];
                [dayDic setValue:model.time forKey:@"time"];
                
                [countDic setValue:model.type forKey:@"type"];
                
                
                NSMutableArray *inOrOutArr = [NSMutableArray array];
                
                [countDic setValue:inOrOutArr forKey:@"countMoney"];
                
                NSDictionary *titleDic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", model.name], @"title", nil];
                NSDictionary *remarksDic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", model.remarks], @"remarks", nil];
                [inOrOutArr addObject:titleDic];
                [inOrOutArr addObject:remarksDic];
                
                if ([model.type isEqualToString:@"1"]) {
                    [dayDic setValue:[NSString stringWithFormat:@"%.2f", model.income.doubleValue] forKey:@"money"];
                }else {
                    [dayDic setValue:[NSString stringWithFormat:@"%.2f", model.money.doubleValue] forKey:@"money"];
                }
                
                allIncome += model.income.doubleValue;
                allPay += model.money.doubleValue;
                [dayArr addObject:dayDic];
            }
            // 将数组倒序
            NSMutableArray *inversedArr = [NSMutableArray array];
            for (NSInteger k = dayArr.count - 1; k >= 0; k--){
                NSDictionary *tempDic = dayArr[k];
                [inversedArr addObject:tempDic];
            }
            dayArr = inversedArr;
            
            // 将每天的第一天附上日期和天数
            for (NSInteger j = 0; j < dayArr.count; j++) {
                NSDictionary *tempDic = dayArr[j];
                if (j == 0) {
                    [tempDic setValue:[NSString stringWithFormat:@"%ld", i] forKey:@"day"];
                    NSDate *weekDate = [[CalendarTool shareCalendarTool] dateFromString:tempStr];
                    NSString *weekStr = [[CalendarTool shareCalendarTool] weekStringFromDate:weekDate];
                    [tempDic setValue:weekStr forKey:@"week"];
                }else {
                    
                }
            }
            
            [cellArr addObject:dayArr];
//            NSLog(@"cellArr: %@", cellArr);
        }
        
        NSMutableDictionary *sectionMoneyDic = [NSMutableDictionary dictionary];
        
        [sectionDic setValue:sectionMoneyDic forKey:@"money"];
        
        [sectionDic setValue:[NSString stringWithFormat:@"%.2f", allIncome - allPay] forKey:@"balance"];
        
        [sectionMoneyDic setValue:[NSString stringWithFormat:@"%.2f", allIncome] forKey:@"1"];
        [sectionMoneyDic setValue:[NSString stringWithFormat:@"%.2f", allPay] forKey:@"2"];
        // detailArr结束
        [newWeekArr addObject:detailArr];
    }
    return newWeekArr;
}


- (NSMutableArray *)detailModalArrayFromDataBaseJudgeByDate:(NSDate *)date {
    NSMutableArray *tempArr = [self arrayFromDataBaseJudgeByDate:date];
    NSMutableArray *newTempArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < tempArr.count; i++) {
        NSArray *weekArr = tempArr[i];
        NSMutableArray *newWeekArr = [DetailModal baseModalByArr:weekArr];
        [newTempArr addObject:newWeekArr];
    }
    return newTempArr;
}



//cell =         (
//    {
//        count =                 {
//            countMoney =                     (
//                                              {
//                                                  title = "model.name";
//                                              },
//                                              {
//                                                  remarks = "model.type";
//                                              }
//                                              );
//            type = "model.type";
//        };
//        day = 2;
//        dayDicName = heiheiehi;
//        image = "model.picName";
//        money = "model.income";
//        week = "\U5468\U4e00";
//    },
//    {
//        count =                 {
//            countMoney =                     (
//                                              {
//                                                  title = "model.name";
//                                              },
//                                              {
//                                                  remarks = "model.type";
//                                              }
//                                              );
//            type = "model.type";
//        };
//        dayDicName = heiheiehi;
//        image = "model.picName";
//        money = "model.income";
//    },
//    {
//        count =                 {
//            countMoney =                     (
//                                              {
//                                                  title = "model.name";
//                                              },
//                                              {
//                                                  remarks = "model.type";
//                                              }
//                                              );
//            type = "model.type";
//        };
//        dayDicName = heiheiehi;
//        image = "model.picName";
//        money = "model.income";
//    },
//    {
//        count =                 {
//            countMoney =                     (
//                                              {
//                                                  title = "model.name";
//                                              },
//                                              {
//                                                  remarks = "model.type";
//                                              }
//                                              );
//            type = "model.type";
//        };
//        dayDicName = heiheiehi;
//        image = "model.picName";
//        money = "model.income";
//    },
//    {
//        count =                 {
//            countMoney =                     (
//                                              {
//                                                  title = "model.name";
//                                              },
//                                              {
//                                                  remarks = "model.type";
//                                              }
//                                              );
//            type = "model.type";
//        };
//        dayDicName = heiheiehi;
//        image = "model.picName";
//        money = "model.income";
//    },
//    {
//        count =                 {
//            countMoney =                     (
//                                              {
//                                                  title = "model.name";
//                                              },
//                                              {
//                                                  remarks = "model.type";
//                                              }
//                                              );
//            type = "model.type";
//        };
//        dayDicName = heiheiehi;
//        image = "model.picName";
//        money = "model.income";
//    },
//    {
//        count =                 {
//            countMoney =                     (
//                                              {
//                                                  title = "model.name";
//                                              },
//                                              {
//                                                  remarks = "model.type";
//                                              }
//                                              );
//            type = "model.type";
//        };
//        dayDicName = heiheiehi;
//        image = "model.picName";
//        money = "model.income";
//        }
//    );
//section =         {
//    balance = 0;
//    money =             {
//        in = 0;
//        out = 0;
//    };
//};
//}

@end
